public class Solution481 {
    public int magicalString(int n) {
        StringBuilder stringBuilder=new StringBuilder();
        int j=2;
        int now=1;
        stringBuilder.append("122");
        while (stringBuilder.length()<n){
            int c=stringBuilder.charAt(j)-'0';
            for (int i=0;i<c;i++){
                stringBuilder.append(now);
            }
            now=3-now;
            j++;
        }
        int ans=0;
        for (int i=0;i<n;i++){
            if (stringBuilder.charAt(i)=='1'){
                ans++;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution481().magicalString(6));
    }
}
